
import * as User from '../../api/user'
import request from '../../request/index.js'
const state = {
    user: [],
    login: []
}
const getters = {

}
const mutations = {
    user(state, data) {
        state.user = data
    },
    login(state, data) {
        state.login = data
    },
    Zc(state, data) {
        console.log(data);
        let flag = true
        if (data.user === '') {
            alert('账号不能为空')
            flag = false
        } else if (data.pass === '') {
            alert('请输入密码')
            flag = false
        } else {
        state.login.forEach(item => {
            if (item.shoujihao === data.user) {
                alert('账号已存在')
                flag = false
            }
        })
        }
        if (flag) {
            request({
                url: '/Zc',
                method: 'POST',
                data: data
            }).then(res => {
                console.log(res);
            })
            alert('注册成功')
        }
    }
}
const actions = {
    async Set(store, data) {
        let res = await User.login()
        console.log(res);
        store.commit('login', res)
    },
    async getNavList(store, data) {
        let res = await User.login()
        console.log(res);
        let un = false, pw = false
        res.forEach(item => {
            if (item.shoujihao === data.user) {
                un = true
                if (item.mima === data.pass) {
                    pw = true
                }
            }
            return;
        })
        if (un && pw) {
            store.commit('user', data)
            return Promise.resolve(res)
        } else if (un && !pw) {
            return Promise.reject('密码错误')
        } else if (!un) {
            return Promise.reject('账号错误或不存在,请重新输入')
        } else {
            return Promise.reject('未知错误')
        }
    },

}
export default {
    namespaced:true,
    state,
    getters,
    mutations,
    actions,
}